/*
 * Copyright 2010 Michael Nitsche
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.mnit.basis.daten.struktur.schluessel.topf;

import java.util.Iterator;

import de.mnit.basis.daten.struktur.S_Folge;
import de.mnit.basis.daten.struktur.gruppe.Gruppe2;
import de.mnit.basis.daten.struktur.menge.Menge;
import de.mnit.basis.daten.struktur.menge.S_Menge;
import de.mnit.basis.daten.struktur.ordnen.Sortieren;
import de.mnit.basis.fehler.Fehler;


/**
 * @author Michael Nitsche
 */
public abstract class A_SchluesselTopf_Basis<TA,TB> implements S_SchluesselTopf_Basis<TA,TB> {

	protected S_Menge<TA> schluessel;


	public A_SchluesselTopf_Basis() {
		this.schluessel = Menge.neu();
	}


	// LESEN

	public boolean istLeer() {
		return schluessel.gLaenge()==0;
	}

	public int gLaenge() {
		return schluessel.gLaenge();
	}

	public int gBreite() {
		return 2;
	}

	public S_Menge<TA> gSchluessel() {
		return schluessel.kopie();
	}

	public Gruppe2<TA, S_Folge<TB>> gib(int zeile) {
		TA ta=schluessel.gib(zeile);
		return Gruppe2.neu(ta, gib(ta));
	}

	public String zuTextDebug() {
		return this.toString();
	}

	// BEFEHL

	public void plusAlle(TA schluessel, Iterable<TB> objekt) {
		for(TB o : objekt) this.plus(schluessel,o);
	}

	public void sortieren(int... nach)   { Sortieren.sortieren(this, nach); }

	public Iterator<Gruppe2<TA, S_Folge<TB>>> iterator() {
		return new Iterator<Gruppe2<TA,S_Folge<TB>>>(){
			int naechster=1;

			public boolean hasNext()   { return gLaenge()>=naechster; }

			public Gruppe2<TA, S_Folge<TB>> next() {
				TA ta=schluessel.gib(naechster++);
				return Gruppe2.neu(ta, gib(ta)); }

			public void remove()   { Fehler.sonstige.da_Untypisch(); }
		};
	}

}
